//config的设置是全局的
layui.config({
    base: '/js/plugins/' // 自定义扩展插件所在的目录
}).extend({ //设定模块别名
    yjsloading: 'yjsloading',
    yjslayer: 'yjslayer',
    yjsupload: 'yjsupload',
});

layui.use(['form','element', 'yjsloading','yjsupload','laydate', 'layedit'], function () {
    var $ = layui.jquery
        , form = layui.form
        , element = layui.element
        , yjsloading = layui.yjsloading
        , yjslayer = layui.yjslayer
        , yjsupload = layui.yjsupload
        , laydate = layui.laydate
        , layedit = layui.layedit;

    // 头像浏览
    layer.photos({
        photos:'#maintainerAvatarViewer'
    });

    // 设置全局参数
    layedit.set({
        tool: ['strong', 'italic', 'underline', 'del', '|', 'left', 'center', 'right', '|', 'link', 'unlink', '|', 'face', 'image'],
    });

    //创建一个编辑器
    var editIndex = layedit.build('maintainers_editor');

    // 自定义表单验证规则
    form.verify({
        name: function (value) {
            if (!/^.{1,20}$/.test(value)) {
                return '维护员姓名长度需在1~20个字符之间';
            }
        },
        checkTelephone: function (value) {
            if (value){
                if (!/^\d{7,8}$/.test(value)){
                    return '电话号码格式不正确';
                }
            }

        },
        checkMobile: function (value) {
            if (value){
                if (!/^(13[0-9]|15[0-9]|18[0-9]|17[0-9])\d{8}$/.test(value)){
                    return  '手机号码格式不正确';
                }
            }
        },
        checkEmail: function (value) {
            if (value){
                if (!/^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/.test(value)){
                    return  '邮箱格式不正确';
                }
            }

        }
    });

    // //日历
    // laydate.render({
    //     elem: '#signCalendar'
    // });


    // 重新获取验证码
    $('.getLVcode').click(function () {
        var url = '/vcode/path/form';
        yjsloading.loadingCaptcha(url);
    });


    // 监听表单确定按钮事件
    form.on('submit(toolbarEvent)', function (data) {
        var eventType = data.field.viewType;
        var url, msg = '';
        switch (eventType) {
            case "addData":
                url = '/maintainers/add';
                msg = '您确定执行本次数据新增操作吗？';
                break;
            case "editData":
                url = '/maintainers/update';
                msg = '您确定执行本次数据更新操作吗？';
                break;
        }

        $title = '维护人员维护';
        $url = url;
        $msg = msg;

        yjsloading.ajaxConfirm($title, $msg, $url, data.field,function (result) {
            $('#rspStatus').val(result['rspStatus']);
            $('#rspMsg').val(result['rspMsg']);
        });
    });

    // 监听表单取消按钮事件
    $('.cancelEvent').click(function () {
        $title = '维护人员维护';
        $msg = '您确定取消本次数据更新操作吗？';
        yjsloading.cancelConfirm($title, $msg);
    });

    // 头像上传事件
    $elem = '#maintainerAvatar';
    $url = '/upload/maintainer/avatar';
    $userCode = $("[name='code']").val();
    $data = {code: $userCode};
    yjsupload.uploadMaintainerAvatar($elem,$url,$data);
});